<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\WarehouseRma;
use App\Services\WarehouseService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class WarehouseRmaController extends Controller
{
    protected $WarehouseService;
    public function __construct(WarehouseService $WarehouseService)
    {
        $this->WarehouseService = $WarehouseService;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $title = $request->title;
        $pageSize = $request->pageSize ?: 10;
        $page = $request->page ?: 1;
        $page = $page * $pageSize - $pageSize;

        $sql = $this->WarehouseService->rmas($title);
        $sql->where('company_id',$request->company_id);
        $sql->where('project_id',$request->project_id);
        $total = $sql->count();
        $data = $sql->skip($page)->take($pageSize)->orderBy('created_at','desc')->get();
        return response()->json(['code' => 200, 'message' => '操作成功','page'=>$page,'pageSize'=>$pageSize,'total'=>$total,'data'=>$data]);
    }

    public function show(Request $request)
    {
        $data = WarehouseRma::find($request->id);
        if($data->rma_date){
            $data->rma_date = strtotime($data->rma_date) * 1000;
        }
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $POSTONLY = WarehouseRma::POSTONLY;
        $input = $request->only($POSTONLY);
        $input['admin_id'] = Auth::user()->id;
        $data = $this->WarehouseService->insertRma($input);
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }
}
